from django.db import models
from django.utils import timezone

from markdown import markdown

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    html_content = models.TextField()
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)
    category =  models.CharField(max_length=100)
    tags = models.CharField(max_length=100)
    views = models.PositiveIntegerField(default=0)

    def save(self, *args, **kwargs):
        # 将 Markdown 内容转换为 HTML
        self.html_content = markdown(self.content)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title

